/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.dataImport.r9.bean;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.yunhoo.base.model.Project;
import com.yunhoo.dataImport.r9.model.RemoteItem;
import com.yunhoo.dataImport.r9.model.FinProjAndConfig;
import com.yunhoo.sys.db.util.IbatisTemplate;
import com.yunhoo.sys.util.common.CommonParam;
import com.yunhoo.sys.util.common.LoginParam;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

@ManagedBean(name = "finDataInterfaceBean")
@SessionScoped
public class FinancialProjectConfig implements Serializable {

    private Project finProj = new Project();
    private DataModel<Project> finProjModel = null;
    private List<RemoteItem> remoteProLi = null;//远程待选项目列表 剔除已使用 
    private String right = "";//配置科目名 字符串
    /*
     * 登录单位 和 会计年度 -- 查询参数
     */
    private String orgCode = null;
    private Integer iyear = null;
    //
    private String nameFilter = "";

    public void selected() {
        finProj = finProjModel.getRowData();
        initList();
        right = "";
        for (RemoteItem item : remoteProLi) {
            if (item.isSelected()) {
                right += item.getName() + ",";
            }
        }
        if (!right.isEmpty()) {
            right = right.substring(0, right.length() - 1);
        }
    }

    private void initList() {
        Map map = new HashMap();
        map.put("orgCode", LoginOrgCode());
        map.put("iyear", loginYear());
        map.put("finCode", finProj.getCode());
        remoteProLi = IbatisTemplate.queryForList("r9DataImport.queryList_remoteItem_finProj", map);
    }

    public void save() {
        if (remoteProLi == null) {
            return;
        }
        String finCode = finProj.getCode();

        List<FinProjAndConfig> insertli = new ArrayList<FinProjAndConfig>();
        for (RemoteItem item : remoteProLi) {
            if (item.isSelected()) {
                FinProjAndConfig data = new FinProjAndConfig(true);
                data.setProjCode(finCode);
                data.setConfigCode(item.getCode());
                insertli.add(data);
            }
        }
//        if (insertli.isEmpty()) {
//            //insertli 为空时 删除当前配置
//            return;
//        }
        SqlMapClient client = IbatisTemplate.getSqlMapClient();
        try {
            client.startTransaction();
            client.startBatch();
            FinProjAndConfig param = new FinProjAndConfig(true);
            param.setProjCode(finCode);
            client.delete("r9DataImport.deleteFinProjConfig", param);
            for (FinProjAndConfig data : insertli) {
                client.insert("r9DataImport.insertFinProjConfig", data);
            }
            client.executeBatch();
            client.commitTransaction();
        } catch (SQLException ex) {
            try {
                client.endTransaction();
            } catch (SQLException ex1) {
                Logger.getLogger(EconomicConfig.class.getName()).log(Level.SEVERE, null, ex1);
            }
            Logger.getLogger(EconomicConfig.class.getName()).log(Level.SEVERE, null, ex);
        }
        initList();
    }

    public void reSelect() {//重选
        for (RemoteItem data : remoteProLi) {
            if (data.isSelected()) {
                data.setSelected(false);
            }
        }
    }

    public DataModel<Project> getFinProjModel() {
        if (finProjModel == null) {
            finProjModel = new ListDataModel<Project>();
            finProjModel.setWrappedData(CommonParam.projectList());
        }
        return finProjModel;
    }

    public Integer loginYear() {
        return iyear == null ? LoginParam.getIyear() : iyear;
    }

    public String LoginOrgCode() {
        return orgCode == null ? LoginParam.getOrg().getCode() : orgCode;
    }

    public Project getFinProj() {
        return finProj;
    }

    public void setFinProj(Project finProj) {
        this.finProj = finProj;
    }

    public List<RemoteItem> getRemoteProLi() {
        return remoteProLi;
    }

    public void setRemoteProLi(List<RemoteItem> remoteProLi) {
        this.remoteProLi = remoteProLi;
    }

    public String getNameFilter() {
        return nameFilter;
    }

    public void setNameFilter(String nameFilter) {
        this.nameFilter = nameFilter;
    }

    public String getRight() {
        return right;
    }

    public void setRight(String right) {
        this.right = right;
    }
}
